;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.InventoryReport", [])
        .controller("InventoryReportCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.datePickerFlag = [false, false];

            $scope.condition = {};
            $scope.dataList = [];

            $scope.switchDatePicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };
            $scope.tbConf = {
                scrollWidth: 1070,
                tableCol: [{
                        type: "",
                        name: "盘点单号",
                        dataField: "checkOrderNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "盘点描述",
                        dataField: "checkOrderDes",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },

                    {
                        type: "",
                        name: "单据状态",
                        dataField: "status",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "仓库",
                        dataField: "warehouseCode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "计划盘点日期",
                        dataField: "plannedDate",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "盘点条码",
                        dataField: "barCode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "储位",
                        dataField: "locationCode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "原始数量",
                        dataField: "sysAmount",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "盘点数量",
                        dataField: "actAmount",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "盘点状态",
                        dataField: "resultType",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "盘点员",
                        dataField: "inventoryUserName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "盘点时间",
                        dataField: "inventoryDate",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "保管员",
                        dataField: "depositaryName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 80,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.sapStatusEnum = [
                {
                    key: 0,
                    value: "未盘点"
                },
                {
                    key: 1,
                    value: "盘点启动"
                },
                {
                    key: 2,
                    value: "盘点结束"
                },
            ];
            $scope.sapState = [{
                key: 2,
                value: "盘盈"
            },
            {
                key: 1,
                value: "盘亏"
            },
            {
                key: 0,
                value: "正常"
            }
        ];

            $scope.yesOrNo = [{
                    key: 1,
                    value: "是"
                },
                {
                    key: 0,
                    value: "否"
                }
            ];

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/inventoryCheck/exportAll"),
                sectionUrl:ybHttp.RequestUrl("device", "/inventoryCheck/export")
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            var btnPermissionCode = {
                export: window.ActionCode.threeReportExport
            };

            $scope.btnPermission = {
                export: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                var service = "device";
                var reqUrl = "/inventoryCheck/listReport";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    angular.forEach($scope.dataList, function (item) {
                        item.inventoryDate = $filter('YbDate')(item.inventoryDate, "yyyy-MM-dd HH:mm:ss");
                        item.plannedDate = $filter('YbDate')(item.plannedDate, "yyyy-MM-dd HH:mm:ss");
                        item.status = $filter("ProgressStatus")(item.status, "inventoryStatus");
                        item.resultType  =  $filter('checkState')(item.resultType );
                    });
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };
            
            /* xbt：仓库查询 */
            $scope.selectWarehouse = function () {
                ybWidget.WarehouseSelector().then(function (data) {
                    $scope.condition.warehouseCode = data.warehouseCode;
                })
            };
            $scope.clearWarehouse = function () {
                $scope.condition.warehouseCode = "";
            };
            (function () {
                getDataList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }]);
})(window, window.angular);